package com.yyfax.ph.entrance;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.main.MainActivity;
import com.yyfax.ph.common.widgets.pagerindicator.InkPageIndicator;

import butterknife.BindView;
import butterknife.OnClick;

public class GuideActivity extends BaseYYFaxActivity {
	@BindView(R.id.guide_viewpager)
	ViewPager mViewPager;
	@BindView(R.id.enter_tv)
	TextView mTextView;
	@BindView(R.id.ink_pager_indicator)
	InkPageIndicator mIndicator;
	private int[] mImage = {R.drawable.ad_top_icon1, R.drawable.ad_top_icon2, R.drawable.ad_top_icon3};

	private AlphaAnimation mAlphaAnimation;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, GuideActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.guide_page_activity_layout;
	}

	@Override
	public int getTopBarBackgroundColor() {
		return getResources().getColor(R.color.color_f5f5f5);
	}

	@Override
	public void initContentView() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setTopBarVisible(false);
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		mIndicator.setViewPager(mViewPager);
		initAnim();

	}

	private void initAnim() {
		mAlphaAnimation = new AlphaAnimation(0.3f, 1);
		mAlphaAnimation.setDuration(400);
	}

	private PagerAdapter mPagerAdapter = new PagerAdapter() {
		@Override
		public int getCount() {
			return mImage.length;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView child = new ImageView(GuideActivity.this);
			child.setImageResource(mImage[position]);
			child.setScaleType(ImageView.ScaleType.CENTER_CROP);
			container.addView(child);
			return child;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	};

	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {
		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			if (position == mImage.length - 1) {
				mTextView.setVisibility(View.VISIBLE);
				mIndicator.setVisibility(View.GONE);
				mTextView.startAnimation(mAlphaAnimation);
			} else {
				mTextView.setVisibility(View.GONE);
				mIndicator.setVisibility(View.VISIBLE);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}
	};

	private void goToMainActivity() {
		MainActivity.start(this);
		finish();
	}

	@OnClick(R.id.enter_tv)
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.enter_tv:
				goToMainActivity();
				break;
		}
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

}
